<template>
	<view class="container">
		<view class="top-wrapper" @click="integralDetails">
			<image src="/static/num-bg.png" class="bg"></image>
			<view class="num-wrapper">
				<image src="/static/num.png" class="icon chil"></image>
				<view class="num chil">{{memberInfo.score}}</view>
				<image src="/static/next05.png" class="next chil"></image>
			</view>
			<view class="desc">可用积分</view>
		</view>
		<view class="product-wrapper">
			<view class="title">兑换专区</view>
			<scroll-view scroll-x="true" class="classification-wrapper" >
				<view class="classification-list">
					<view class="classification-item" :class="{'on': type.id == catId}" v-for="type in typeList" @click="handleSwitchType(type)" :key="type.id">{{type.name}}</view>
				</view>
			</scroll-view>
			<view class="product-list">
				<product v-for="product in list" :product="product" :key="product.id"></product>
			</view>
		</view>
		<view class='no-data' v-if="list.length < 1 && isLoad">暂无数据</view>
		<view class='end' v-if="loadingFinsh && list.length > 0">没有更多了</view>
	</view>
</template>

<script>
	import product from './product.vue'
	import { frontPage, selectByType } from '@/api/product.js'
	export default {
		components: {product},
		data() {
			return {
				typeList: [],
				catId: '',
				type: 1, // 1.积分商品 2.特价商品 3.商户超市商品 4.商户服务 5.售货机商品
				list: [],
				isLoad: false,
				loadingFinsh: false,
				loadingShow: false,
				totalCount: 0,
				pageNum: 1,
				pageSize: 10,
			}
		},
		computed: {
			memberInfo () {
			  return this.$store.getters.userInfo
			}
		},
		onLoad() {
			this.getTypeList()
		},
		onShow() {
			this.$store.dispatch('user/getUserInfo')
		},
		onReachBottom() {
			this.pageNum = this.pageNum + 1
			this.getList()
		},
		methods: {
			getTypeList() {
				const data = {
					type: this.type
				}
				selectByType(data).then(respanse => {
					this.typeList = respanse.result
					if (respanse.result.length > 0) {
						this.catId = respanse.result[0].id
						this.getList()
					}
				})
			},

			getList() {
				const len = this.list.length
				if (this.totalCount == len && this.isLoad) {
				  this.loadingFinsh = true,
				  this.loadingShow = false
				  return
				}
				this.loadingShow = true
				const data = {
					catId: this.catId,
					pageNum: this.pageNum,
					pageSize: this.pageSize,
					type: this.type
				}
				frontPage(data).then(respanse => {
					const { rows, total } = respanse.result
					if (rows) {
						this.list = this.list.concat(rows)
					}
					this.totalCount = total
					this.isLoad = true
					this.loadingShow = false
				})
			},

			//  切换分类
			handleSwitchType(item) {
				this.catId = item.id
				this.list = []
				this.isLoad = false
				this.loadingFinsh = false
				this.loadingShow = false
				this.totalCount = 0
				this.pageNum = 1
				this.getList()
			},

			integralDetails() {
				uni.navigateTo({
				    url: `/pages/integral/integral-details`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.top-wrapper{
		position: relative;
		margin: 21rpx 25rpx 0;
		padding-top: 49rpx;
		height: 279rpx;
		box-sizing: border-box;
		.bg{
			position: absolute;
			left: 0;
			top: 0;
			z-index: -1;
			width: 100%;
			height: 100%;
		}
		.num-wrapper{
			padding: 10rpx 0;
			text-align: center;
			.icon{
				display: inline-block;
				vertical-align: bottom;
				margin: 0 22rpx 8rpx 0;
				width: 37rpx;
				height: 38rpx;
			}
			.num{
				display: inline-block;
				vertical-align: middle;
				line-height: 1;
				font-size: 72rpx;
				color: #fff;
			}
			.next{
				margin-left: 24rpx;
				display: inline-block;
				vertical-align: middle;
				width: 19rpx;
				height: 33rpx;
			}
		}
		.desc{
			margin-top: 23rpx;
			line-height: 32rpx;
			text-align: center;
			font-size: 22rpx;
			color: rgba(255, 255, 255, 0.68);
		}
	}
	.product-wrapper{
		padding: 42rpx 0 0 25rpx;
		.title{
			position: relative;
			display: table;
			line-height: 1;
			font-size: 40rpx;
			color: #333333;
			&::after{
				content: " ";
				position: absolute;
				left: 0;
				right: 0;
				bottom: 0;
				height: 8rpx;
				background: #25C477;
				opacity: 0.5;
			}
		}
		.classification-wrapper{
			margin: 36rpx 0 35rpx;
			.classification-list{
				display: flex;
				.classification-item{
					padding: 0 34rpx;
					margin-right: 27rpx;
					white-space: nowrap;
					height: 58rpx;
					line-height: 58rpx;
					background: #E9E9E9;
					border-radius: 29rpx;
					font-size: 30rpx;
					color: #010101;
					&.on{
						color: #fff;
						background-color: #EA5D2F;
					}
				}
			}
		}
	}
	.product-list{
		display: flex;
		padding-right: 25rpx;
		flex-wrap: wrap;
		justify-content: space-between;
	}
</style>
